# -*- coding: UTF-8 -*-

import logging

from google.appengine.api import users
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from models import Greeting

def index(request):
    greetings = Greeting.all().order('-update_time')
        
    logging.debug('beging....::%s' % '测试2222')
    
    if users.get_current_user():
        logging.debug(users.get_current_user())
        url = users.create_logout_url(request.path)
        url_linktext = 'Logout'
    else:
        url = users.create_login_url(request.path)
        url_linktext = 'Login'

    template_values = {
        'greetings': greetings,
        'url': url,
        'url_linktext': url_linktext,
    }

    return render_to_response('guestbook/index.html', template_values)
    
def saveAction(request):
    greeting = Greeting()

    if users.get_current_user():
        greeting.author_type = 'google'
        greeting.google_author = users.get_current_user()
    else:
        greeting.author_type = 'other'
        greeting.author_name = request.POST['name']
        greeting.author_url = request.POST['url']
        greeting.author_mail = request.POST['mail']

    greeting.content = request.POST['content'].decode('UTF-8')
    logging.debug("--------Greeting : %s" % greeting)
    greeting.put()
    return HttpResponseRedirect('/guestbook')
